package com.ruoyi.web.controller.sealjur;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SealInfo;
import com.ruoyi.common.core.domain.entity.SealJur;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISealInfoService;
import com.ruoyi.system.service.ISysUserService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISealJurService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 印章授权Controller
 * 
 * @author ess
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/sealjur/sealJur")
public class SealJurController extends BaseController
{
    @Autowired
    private ISealJurService sealJurService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISealInfoService sealInfoService;


    /**
     * 查询印章授权列表
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:list')")
    @GetMapping("/list")
    public TableDataInfo list(SealJur sealJur)
    {
        startPage();
        List<SealJur> list = sealJurService.selectSealJurList(sealJur);
        return getDataTable(list);
    }

    /**
     * 导出印章授权列表
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:export')")
    @Log(title = "印章授权", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SealJur sealJur)
    {
        List<SealJur> list = sealJurService.selectSealJurList(sealJur);
        ExcelUtil<SealJur> util = new ExcelUtil<SealJur>(SealJur.class);
        return util.exportExcel(list, "印章授权数据");
    }

    /**
     * 获取印章授权详细信息
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:query')")
    @GetMapping(value = "/{sealJurId}")
    public AjaxResult getInfo(@PathVariable("sealJurId") String sealJurId)
    {
        return AjaxResult.success(sealJurService.selectSealJurBySealJurId(sealJurId));
    }

    /**
     * 新增印章授权
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:add')")
    @Log(title = "印章授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SealJur sealJur)
    {

        if (StringUtils.isEmpty(sealJur.getSealPersonId())) {
            return AjaxResult.error("授权失败，被授权人不能为空，请搜索添加");
        }
        //        判断到期时间是否合规
        //        判断跟当前时间
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dueTimeDate = null;
        try {
            dueTimeDate = sf.parse(sealJur.getDueTime());
        } catch (ParseException e) {
            e.printStackTrace();
            
            return AjaxResult.error("授权失败，后端出现异常");
        }
        if (dueTimeDate.before(date)) {
            return AjaxResult.error("授权失败，转授到期时间错误");
        }


        //        判断是否已经授权过
        if (sealJurService.checkAreadyJur(sealJur)) {
            return AjaxResult.error("授权失败，已对此人授权过此");
        }

        LoginUser loginUser = getLoginUser();
        sealJur.setJurUserId(String.valueOf(loginUser.getUser().getUserId()));
        return toAjax(sealJurService.insertSealJur(sealJur));
    }

    /**
     * 修改印章授权
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:edit')")
    @Log(title = "印章授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SealJur sealJur)
    {
        return toAjax(sealJurService.updateSealJur(sealJur));
    }

    /**
     * 删除印章授权
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:remove')")
    @Log(title = "印章授权", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sealJurIds}")
    public AjaxResult remove(@PathVariable String[] sealJurIds)
    {
        return toAjax(sealJurService.deleteSealJurBySealJurIds(sealJurIds));
    }



    /**
     * 获取签章用户列表  授权处
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:signuserlist')")
    @GetMapping("/signuserlist")
    public TableDataInfo signuserlist(SysUser user) {
        startPage();
        List<SysUser> list = userService.selectSignUserList(user);
        return getDataTable(list);
    }


    /**
     * 查询印章列表 授权处
     */
    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:seallist')")
    @GetMapping("/sealList")
    public TableDataInfo sealList(SealInfo sealInfo)
    {
        startPage();
        List<SealInfo> list = sealInfoService.selectSealInfoListSealJur(sealInfo);
        return getDataTable(list);
    }

//    /**
//     * 根据人员id查找手签手签数据
//     */
////    @PreAuthorize("@ss.hasPermi('sealjur:sealJur:query')")
//    @PostMapping(value = "/querySealJurByPersonId")
//    public AjaxResult querySealJurByPersonId(@RequestBody String personId)
//    {
//
//         SealJur sealJur  = sealJurService.querySealJurByPersonId(personId);
//         if (sealJur==null)
//        {
//            return AjaxResult.error("此用户暂未制作手签");
//        }
//
//        return AjaxResult.success(sealJur);
//    }

}
